<?php
//会员实体类
class UserModel extends Model {
    public $id;
    public $openid;
    public $name;
    public $phone;
    public $like_count;
    public $create_date;


    //查找单一会员
    public function getOneUser() {
        $_sql = "SELECT
											id,
											openid,
											name,
											phone,
											like_count,
											create_date
								FROM 
											wx_user
							WHERE 
											id='$this->id' 
								LIMIT 
											1";
        return parent::one($_sql);
    }

    //根据openid修改用户的点赞数
    public function updateLikeCountToOpenid() {
        $_sql = "UPDATE
											wx_user
									SET
											like_count='$this->like_count'

							WHERE
											openid='$this->openid'";
        return parent::aud($_sql);
    }

    //删除会员
    public function deleteUser() {
        $_sql ="DELETE FROM
												wx_user
										WHERE 
												id='$this->id' 
										LIMIT 1";
        return parent::aud($_sql);
    }


    /**
     * 根据openid获取like_count
     * @param $openid
     * @return mixed
     */
    public function getLikeCountToOpenid($openid) {
        $_sql = "SELECT
											like_count
								FROM
											wx_user
							WHERE
								            openid ='$openid'
								LIMIT
											1";
        $_db = DB::getDB();
        $_result = $_db->query($_sql);
        $_objects = $_result->fetch_object();
        DB::unDB($_result, $_db);
        return $_objects->like_count;
    }


    //获取会员总量
    public function getUserTotal() {
        $_sql = "SELECT
										COUNT(id)
								FROM 
										wx_user";

        return parent::total($_sql);
    }

    //获取会员总量
    public function getSurplusUserTotal() {
        $_sql = "SELECT
								(400 - (select COUNT(id) from wx_user where LIKE_count >= 20) )";
        return parent::total($_sql);
    }

    //获取总点赞数
    public function getLikeTotal() {
        $_sql = "SELECT
										COUNT(id)
								FROM
										wx_like";
        return parent::total($_sql);
    }

    //获取不重复微信用户点赞
    public function getNoRepeatLikeTotal() {
        $_sql = "SELECT
										COUNT( distinct like_person_openid)
								FROM
										wx_like";
        return parent::total($_sql);
    }

    //按照like_count 倒序查找20条记录
    public function getUserOrderByLikeCount() {
        $_sql = "SELECT
                        name,
                        like_count
                FROM
                        wx_user
                 ORDER BY
                        like_count DESC
                 LIMIT 0,20";
        return parent::all($_sql);
    }

    // 获取 昵称 手机号 得票数
    public function getUserNamePhoneLikeCount() {
        $_sql = "SELECT `name`, `phone`, `like_count` FROM wx_user ORDER BY like_count DESC, create_date ASC";
        return parent::all($_sql);
    }







    //手机号码重复
    public function checkUserToPhoneOrOpenid() {
        $_sql = "SELECT
											id
								FROM 
											wx_user
							WHERE 
											phone='$this->phone'
								            OR
								            openid ='$this->openid'
								LIMIT 
											1";
        $_db = DB::getDB();
        $_result = $_db->query($_sql);
        $_objects = $_result->fetch_object();
        DB::unDB($_result, $_db);
        return $_objects->id;
    }

    // 根据openid
    public function checkUserToOpenid() {
        $_sql = "SELECT
											id
								FROM
											wx_user
							WHERE
								            openid ='$this->openid'
								LIMIT
											1";
        $_db = DB::getDB();
        $_result = $_db->query($_sql);
        $_objects = $_result->fetch_object();
        DB::unDB($_result, $_db);
        return $_objects->id;
    }

    //新增会员
    public function addUser() {
        $_sql = "INSERT INTO
                wx_user (
                            openid,
                            name,
                            phone,
                            like_count,
                            create_date
                                        )
                        VALUES (
                        '$this->openid',
                        '$this->name',
                        '$this->phone',
                        0,
                        NOW()
                         )";
        return parent::aud($_sql);
    }


    /**
     * 点赞
     */
    public function addLike(&$like_person_openid,&$bei_like_openid){
        $_sql = "INSERT INTO
                wx_like (
                            like_person_openid,
                            bei_like_openid,
                            create_date
                                        )
                        VALUES (
                        '$like_person_openid',
                        '$bei_like_openid',
                        NOW()
                         )";
        return parent::aud($_sql);

    }


    /**
     * 是否已点赞
     */
    public function existsLike(&$like_person_openid,&$bei_like_openid){
        $_sql = "SELECT
											id
								FROM
											wx_like
							WHERE
								            like_person_openid ='$like_person_openid'
								            AND
								            bei_like_openid='$bei_like_openid'
								LIMIT
											1";
        $_db = DB::getDB();
        $_result = $_db->query($_sql);
        $_objects = $_result->fetch_object();
        DB::unDB($_result, $_db);
        return $_objects->id;


    }
}
?>